package com.iseastar.guojiang.team.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;

import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.CourierTeamDetailBackBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.team.adapter.CourierTeamDetailMonthAdapter;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.SListView;

/**
 * @author 李澄锋<br>
 * 2018/6/12
 */
public class TeamCenterDetailMonthOrderNumFragment extends BaseFragment {

    private CourierTeamDetailMonthAdapter mAdapter = null;
    private SListView mListView = null;
    private int type;//0 按月单量  1 按月里程  2 平均月收入
    private int teamId;
    private boolean isRequest = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getInt("type");
            teamId = bundle.getInt("teamId");
        }
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().getTeamDetailList(teamId, type + 1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.fragment_team_detail_month_order_num);
        super.findViewById();
        mListView = (SListView) findViewById(R.id.listview);
        mAdapter = new CourierTeamDetailMonthAdapter(null, getActivityContext());
        mListView.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().getTeamDetailList(teamId, type + 1);
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.courier_team_detail_list:
                cancelLoadingDialog();
                onRefreshComplete();
                final ReqResult<CourierTeamDetailBackBean> reqResult = JSON.parser(msg.obj, CourierTeamDetailBackBean.class);
                if (checkLoginStatus(reqResult)) {
                    int arg1 = msg.arg1;
                    if (arg1 == type + 1) {
                        getActivityContext().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                CourierTeamDetailBackBean backBean = reqResult.getResult();
                                if (backBean != null) {
                                    if (mAdapter == null) {
                                        mAdapter = new CourierTeamDetailMonthAdapter(backBean.getCourierList(), getActivityContext());
                                        mListView.setAdapter(mAdapter);
                                    } else {
                                        mAdapter.setItems(backBean.getCourierList(), true);
                                    }
                                }
                            }
                        });
                    }
                } else {
                    showToast(reqResult.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isRequest){
            showLoadingDialog(null);
            AppHttp.getInstance().getTeamDetailList(teamId, type + 1);
        }
        isRequest = false;
    }

    @Override
    public void onStop() {
        super.onStop();
        isRequest = true;
    }

    /**
     * 刷新列表数据
     */
    public void refreshData(int teamId,int type){
        showLoadingDialog(null);
        AppHttp.getInstance().getTeamDetailList(teamId, type + 1);
    }
}
